//读取游戏
onLoad( function( body ){

	//装载库
	Box = Z.element.Box;
	Label = Z.element.Label;
	Timer = body.Timer;

	//全局Area
	var globalArea = body.area( {
		left: 0,
		top: 0,
		width: 1920,
		height: 1080
	} );
	//游戏区
	var SCREEN_WIDTH = 720;
	var SCREEN_HEIGHT = 480;
	var gameBorder = Box( globalArea, {
		border_width: "1px",
		border_color: "#404040",
		border_style: "solid",
		left: 9,
		top: 9,
		width: SCREEN_WIDTH,
		height: SCREEN_HEIGHT,
		zIndex: 10
	} );
	var gameScreen = globalArea.area( {
		left: 10,
		top: 10,
		width: SCREEN_WIDTH,
		height: SCREEN_HEIGHT,
		zIndex: 20
	} );

	//场景
	var scene = "tip";
	//提示场景
	var tipScene = gameScreen.area( {
		left: 0,
		top: 0,
		width: SCREEN_WIDTH,
		height: SCREEN_HEIGHT
	} );
	//平时场景
	var peaceScene = gameScreen.area( {
		left: 0,
		top: 0,
		width: SCREEN_WIDTH,
		height: SCREEN_HEIGHT
	} );
	peaceScene.visible( false );
	//战时场景
	var battleScene = gameScreen.area( {
		left: 0,
		top: 0,
		width: SCREEN_WIDTH,
		height: SCREEN_HEIGHT
	} );
	battleScene.visible( false );

	//提示语
	var tipLabel = Label( tipScene, {
		left: 0,
		top: 0,
		zIndex: 10
	} );
	//游戏初始化
	tipLabel.text( "点击鼠标开始游戏" );
	tipLabel.left( ( SCREEN_WIDTH-tipLabel.offsetWidth( ) )/2 );
	tipLabel.top( ( SCREEN_HEIGHT-tipLabel.offsetHeight( ) )/2 );
	//开始按钮
	var startBtn = tipScene.area( {
		left: 0,
		top: 0,
		width: SCREEN_WIDTH,
		height: SCREEN_HEIGHT,
		zIndex: 20,
		onClick: function( ){
			//切换场景
			tipScene.visible( false );
			peaceScene.visible( true );
			scene = "peace";
			//开始对话
			nextConversation( );
			//清理
			tipLabel.text( "" );
			startBtn.remove( );
		}
	} );

	//===================================================和平部分=======================================

	//对话面板
	var DIALOGUE_GAP = 120;
	var dialogue = {
		"top": Dialogue( peaceScene, SCREEN_WIDTH-DIALOGUE_WIDTH-DIALOGUE_GAP, -DIALOGUE_HEIGHT ),
		"bottom": Dialogue( peaceScene, DIALOGUE_GAP, SCREEN_HEIGHT )
	};
	var dialoguePath = {
		"top": accelerationTrace( 0, 0, -4, 12 ),
		"bottom": accelerationTrace( SCREEN_HEIGHT-DIALOGUE_HEIGHT, 0, 4, 12 )
	};

	//头像
	var portrait = {
		"top": Portrait( peaceScene, SCREEN_WIDTH, 0 ),
		"bottom": Portrait( peaceScene, -PORTRAIT_WIDTH, SCREEN_HEIGHT-PORTRAIT_HEIGHT )
	};
	var portraitPath = {
		"top": accelerationTrace( SCREEN_WIDTH-PORTRAIT_WIDTH, 0, 3, 15 ),
		"bottom": accelerationTrace( 0, 0, -3, 15 )
	};

	//“点击继续”标志
	var PUSH_WIDTH = 100;
	var PUSH_HEIGHT = 100;
	var pushImages = [ Box( peaceScene, {
		left: SCREEN_WIDTH-PUSH_WIDTH,
		top: SCREEN_HEIGHT-PUSH_HEIGHT,
		width: PUSH_WIDTH,
		height: PUSH_HEIGHT,
		background_image: "url(img/push0.png)",
		zIndex: 100,
		visible: false
	} ), Box( peaceScene, {
		left: SCREEN_WIDTH-PUSH_WIDTH,
		top: SCREEN_HEIGHT-PUSH_HEIGHT,
		width: PUSH_WIDTH,
		height: PUSH_HEIGHT,
		background_image: "url(img/push1.png)",
		zIndex: 100,
		visible: false
	} ) ];
	var pushAnime = CycleAnime( pushImages, 500 );

	//场景计数
	var dialogueCount = 0;
	//对话计数
	var talkCount = -1;

	//当前动作
	var currentTalk = {
		"top": null,
		"bottom": null
	};

	//过场动画
	var interlude = Playlist( );
	//进行下一步对话
	var nextConversation = function( ){
		//有动画执行中
		if(!interlude.empty( )){
			interlude.skip( );
		}
		//响应点击
		else{
			//隐藏“等待点击”动画
			pushAnime.remove( );
			//步进
			talkCount++;
			//存在对话
			if(dialogueCount<dialogues.length){
				//取出对话
				var talks = dialogues[dialogueCount];
				//存在下一条
				if(talkCount<talks.length){
					//取出这条动作
					var talk = talks[talkCount];
					//动作的位置
					var pos = "";
					//存在新位置
					if(talk["position"]){
						//保存位置和内容
						pos = talk["position"];
						currentTalk[pos] = talk;
						//设置新头像
						portrait[pos].background_image( chara[talk["chara"]].portrait[talk["emotion"]] );

						var doneDialogue = false;
						var donePortrait = false;
						//飞入之后的动作
						var afterIn = function( ){
							if(!doneDialogue){
								return;
							}
							if(!donePortrait){
								return;
							}
							//显示文字
							interlude.play( 50, Typer( dialogue[pos], talk["talk"] ), pushAnime.play );
						};
						//对话框飞入
						interlude.play( 50, Motion( dialogue[pos].top, dialoguePath[pos].reverse( ) ), function( ){
							doneDialogue = true;
							afterIn( );
						} );
						//头像飞入
						interlude.play( 50, Motion( portrait[pos].left, portraitPath[pos].reverse( ) ), function( ){
							donePortrait = true;
							afterIn( );
						} );
					}
					//无新位置
					else{
						//寻找角色对应的位置
						if(currentTalk["top"]!=null&&talk["chara"]==currentTalk["top"]["chara"]){
							pos = "top";
						}
						else if(currentTalk["bottom"]!=null&&talk["chara"]==currentTalk["bottom"]["chara"]){
							pos = "bottom";
						}
						//如是退场动作则退场
						if(talk["action"]=="out"){
							//清空对话
							dialogue[pos].text( "" );

							var donePortrait = false;
							var doneDialogue = false;
							//飞出之后的动作
							var afterOut = function( ){
								if(!donePortrait){
									return;
								}
								if(!doneDialogue){
									return;
								}
								//动画完毕解锁
								lock = false;
								//直接执行下一动作
								nextConversation( );
							};
							//头像飞出
							interlude.play( 50, Motion( portrait[pos].left, portraitPath[pos].reverse( ) ), function( ){
								donePortrait = true;
								afterOut( );
							} );
							//对话框飞出
							interlude.play( 50, Motion( dialogue[pos].top, dialoguePath[pos].reverse( ) ), function( ){
								doneDialogue = true;
								afterOut( );
							} );
						}
						//否则继续执行
						else{
							//存在新表情则设定
							if(talk["emotion"]){
								portrait[pos].background_image( chara[talk["chara"]].portrait[talk["emotion"]] );
							}
							//显示文字
							interlude.play( 50, Typer( dialogue[pos], talk["talk"] ), pushAnime.play );
						}
					}
				}
				//对话结束
				else{
					//重置对话
					talkCount = -1;
					dialogueCount++;
					//切换场景
					peaceScene.visible( false );
					battleScene.visible( true );
					scene = "battle";
					openCombat( );
				}
			}
			//否则结束游戏
			else{
				//切换场景
				peaceScene.visible( false );
				tipScene.visible( true );
				scene = "tip";
				//游戏结束提示
				tipLabel.text( "The End" );
				tipLabel.left( ( SCREEN_WIDTH-tipLabel.offsetWidth( ) )/2 );
				tipLabel.top( ( SCREEN_HEIGHT-tipLabel.offsetHeight( ) )/2 );
			}
		}
	};
	//点击进行下一步
	var nextBtn = peaceScene.area( {
		left: 0,
		top: 0,
		width: SCREEN_WIDTH,
		height: SCREEN_HEIGHT,
		zIndex: 30,
		onClick: nextConversation
	} );
	//跳过动作
	var skip = Timer( function( ){
		if(scene=="peace"){
			nextConversation( );
		}
	}, PRESS_INTERVAL );
	//快进
	body.onKeydown( function( e ){
		switch(e.keyCode){
		case VKEY_SPACE:
			skip.start( );
			break;
		}
	} );
	body.onKeyup( function( e ){
		switch(e.keyCode){
		case VKEY_SPACE:
			skip.stop( );
			break;
		}
	} );

	//===================================================战斗部分=======================================

	//战场
	var battleField = battleScene.area( {
		left: 0,
		top: 0,
		width: 0,
		height: 0
	} );
	//战场背景
	var bg = Box( battleField, {
		left: 0,
		top: 0,
		width: 0,
		height: 0
	} );
	//滚屏动作
	var SCROLL_VGAP = 15;
	var SCROLL_HGAP = 25;
	var scroll = {
		"up": Timer( function( ){
			if(scene=="battle"&&battleField.top( )<0){
				battleField.top( battleField.top( )+SCROLL_VGAP );
			}
		}, PRESS_INTERVAL ),
		"down": Timer( function( ){
			if(scene=="battle"&&battleField.top( )>SCREEN_HEIGHT-battleField.offsetHeight( )){
				battleField.top( battleField.top( )-SCROLL_VGAP );
			}
		}, PRESS_INTERVAL ),
		"left": Timer( function( ){
			if(scene=="battle"&&battleField.left( )<0){
				battleField.left( battleField.left( )+SCROLL_HGAP );
			}
		}, PRESS_INTERVAL ),
		"right": Timer( function( ){
			if(scene=="battle"&&battleField.left( )>SCREEN_WIDTH-battleField.offsetWidth( )){
				battleField.left( battleField.left( )-SCROLL_HGAP );
			}
		}, PRESS_INTERVAL )
	};
	//鼠标滚屏
	var lastLeft = 0;
	var lastTop = 0;
	globalArea.onMousemove( function( e ){
		//每个方向的条件
		var direct = [ "up", "down", "left", "right" ];
		var previous = {
			"up": lastTop,
			"down": -lastTop,
			"left": lastLeft,
			"right": -lastLeft
		};
		var present = {
			"up": e.clientY,
			"down": -e.clientY,
			"left": e.clientX,
			"right": -e.clientX
		};
		var border = {
			"up": gameScreen.top( )+SCROLL_VGAP,
			"down": -( gameScreen.top( )+SCREEN_HEIGHT-SCROLL_VGAP ),
			"left": gameScreen.left( )+SCROLL_HGAP,
			"right": -( gameScreen.left( )+SCREEN_WIDTH-SCROLL_HGAP )
		};
		//各方向判断
		foreach( direct, function( d ){
			if(previous[d]>=border[d]&&present[d]<border[d]){
				scroll[d].start( );
			}
			else if(previous[d]<border[d]&&present[d]>=border[d]){
				scroll[d].stop( );
			}
		} );
		//更新
		lastLeft = e.clientX;
		lastTop = e.clientY;
	} );
	//键盘滚屏
	body.onKeydown( function( e ){
		switch(e.keyCode){
		case VKEY_W:
			scroll["up"].start( );
			break;
		case VKEY_S:
			scroll["down"].start( );
			break;
		case VKEY_A:
			scroll["left"].start( );
			break;
		case VKEY_D:
			scroll["right"].start( );
			break;
		}
	} );
	body.onKeyup( function( e ){
		switch(e.keyCode){
		case VKEY_W:
			scroll["up"].stop( );
			break;
		case VKEY_S:
			scroll["down"].stop( );
			break;
		case VKEY_A:
			scroll["left"].stop( );
			break;
		case VKEY_D:
			scroll["right"].stop( );
			break;
		}
	} );

	//战斗计数
	var battleCount = 0;
	//战斗用小动画
	var battlePlaylist = Playlist( );
	//战斗设定值
	var battle = null;
	//可动战斗主体
	var masters = [ ];
	//战斗地图
	var battleMap = null;
	//回合数记录
	var turnCount = -1;
	//回合结束事件
	var turnEnd = function( ){
		turnCount++;
		if(turnCount>battle.turns){
			battleEnd( );
		}
		else{
			masters[turnCount%masters.length].act( );
		}
	};
	//战斗结束事件
	var battleEnd = function( ){
		//切换场景
		battleScene.visible( false );
		peaceScene.visible( true );
		scene = "peace";
		//开始对话部分
		nextConversation( );
		//重置战斗部分
		battleMap.remove( );
		battleCount++;
		turnCount = -1;
	};
	//初始化场景
	var openCombat = function( ){
		//有剩余战斗
		if(battleCount<battles.length){
			//读取信息
			battle = battles[battleCount];
			//设置地图
			battleMap = Map( battleField, battle.cols, battle.rows, 0, 0 );
			//设置背景
			bg.background_image( battle.bg );
			bg.width( battleField.offsetWidth( ) );
			bg.height( battleField.offsetHeight( ) );
			//开始
			turnEnd( );
		}
		//否则游戏结束
		else{
			//切换场景
			battleScene.visible( false );
			tipScene.visible( true );
			scene = "tip";
			//游戏结束提示
			tipLabel.text( "The End" );
			tipLabel.left( ( SCREEN_WIDTH-tipLabel.offsetWidth( ) )/2 );
			tipLabel.top( ( SCREEN_HEIGHT-tipLabel.offsetHeight( ) )/2 );
		}
	};
	//向角色列表中添加一个火球
	masters.push( ( function( ){
		//火球原本的位置
		var lastX = 0;
		var lastY = 0;
		return {
			//回合起始动作：建立火球点击发射装置
			act: function( ){
				//建立射程选择区，点击发射
				var range = FireBallRange( battleMap, {
					x: lastX,
					y: lastY
				}, function( target ){
					//消除射程区
					range.remove( );
					//读取目标
					var x = target[0].x;
					var y = target[0].y;
					//火球发射动画
					var centerLeftTop = function( x, y ){
						return {
							x: x*TILE_SLIDE_WIDTH+TILE_SLIDE_WIDTH,
							y: y*TILE_SLIDE_HEIGHT+TILE_SLIDE_HEIGHT
						};
					};
					var fireball = FireBall( battleField, centerLeftTop( lastX, lastY ), centerLeftTop( x, y ), 100 );
					//目标地点居中
					//计算X
					var finalX = ( SCREEN_WIDTH-TILE_WIDTH )/2-x*TILE_SLIDE_WIDTH;
					finalX = finalX<0? finalX : 0;
					finalX = finalX>SCREEN_WIDTH-battleField.offsetWidth( )? finalX : SCREEN_WIDTH-battleField.offsetWidth( );
					var pathX = accelerationPath( battleField.left( ), finalX, 10 );
					//计算Y
					var finalY = ( SCREEN_HEIGHT-TILE_HEIGHT )/2-y*TILE_SLIDE_HEIGHT;
					finalY = finalY<0? finalY : 0;
					finalY = finalY>SCREEN_HEIGHT-battleField.offsetHeight( )? finalY : SCREEN_HEIGHT-battleField.offsetHeight( );
					var pathY = accelerationPath( battleField.top( ), finalY, 10 );
					//拼合路径
					var path = [ ];
					loop( 10, function( i ){
						path.push( {
							x: pathX[i],
							y: pathY[i]
						} );
					} );
					//发射火球->追踪移动->结束回合
					fireball.play( battlePlaylist, function( ){
						battlePlaylist.play( 50, Motion( function( point ){
							battleField.left( point.x );
							battleField.top( point.y );
						}, path ), turnEnd );
					} );
					//更新火球位置
					lastX = x;
					lastY = y;
				} );//range
			}//act
		};//return
	} )( ) );

} );